define([ './module' ], function(productsmodule) {
	'use strict';

	productsmodule.controller('products',productsctrl);

	function productsctrl($stateParams,$scope,$http,$location,Upload,$timeout,GoodsTypeService,goodscategoryService,productskuService){
		//查询所有商品类型
		$scope.findallgoodstype=function(){
			var promise=GoodsTypeService.query();
			promise.then(function(data){
				$scope.goodstypeselect=data.beanlist;
			},function(data){
				$scope.errors={
					show:true,
					msg:'系统异常'
				}
			});
		};

		//选择商品类型事件
		$scope.goodstypechange=function(){
			if($scope.selectedgoodstype!=undefined){
				$http({
					method:'POST',
					url:'../mall/goodstypebrand/findAllTypeBrands',
					data:{
						'goodstypeid':$scope.selectedgoodstype.goodstypeid
					}
				}).success(function(data,status,headers,config){
					if(data.sucflag){
						$scope.errors={};
						$scope.brandsselect=[];
						$scope.brandsselect=angular.fromJson(data.beanlist[0].brandlist);
					}
				}).
				error(function(data,status,headers,config){
					$scope.errors={
						show:true,
						msg:'系统异常'
					}
				});

			};
			//填充商品类型的参数
			$scope.gtparams=[];
			$scope.gtparams=angular.fromJson($scope.selectedgoodstype.goodsParameter);
			$scope.findAllTypeGoodsAttrs($scope.selectedgoodstype.goodstypeid);
		};


		/**
		 * 查询商品属性
		 */
		$scope.findAllTypeGoodsAttrs=function(goodstypeid){
			$http({
				method:'POST',
				url:'../mall/goodsattribute/findAllTypeGoodsAttrs',
				data:{
					'goodstypeid':goodstypeid
				}
			}).success(function(data,status,headers,config){
				if(data.sucflag){
					$scope.errors={};
					$scope.goodsattrs=[];
					$scope.attrslist=[];
					angular.forEach(data.beanlist,function(d,i,a){
						$scope.attrslist.push(angular.fromJson(d.attributelist));
						$scope.goodsattrs.push(d);
					});

				}
			}).
			error(function(data,status,headers,config){
				$scope.errors={
					show:true,
					msg:'系统异常'
				}
			});
		};

		//获取所有一级商品分类
		$scope.findNavAll=function(){
			var promise=goodscategoryService.query();
			promise.then(function(data){
				$scope.navselect=data.beanlist;
			},function(data){
				$scope.errors={
					show:true,
					msg:'系统异常'
				}
			});
		};
		//获取所有二级商品分类
		$scope.navchange=function(){
			if($scope.selectednav!=undefined){
				$http({
					method:'POST',
					url:'../mall/goodscategory/findCategoryAllByParentId',
					data:{
						'id':$scope.selectednav.id
					}
				}).success(function(data,status,headers,config){
					if(data.sucflag&&data.beanlist.length>0){
						$scope.errors={};
						$scope.ltypeselect=[];
						$scope.ltypeselect=data.beanlist;
						$scope.ltypecategoryshow=true;
					}else{
						$scope.ltypecategoryshow=false;
					}
				}).
				error(function(data,status,headers,config){
					$scope.errors={
						show:true,
						msg:'系统异常'
					}
				});

			};
		};
		//获取所有三级商品分类
		$scope.ltypechange=function(){
			if($scope.selectedltype!=undefined){
				$http({
					method:'POST',
					url:'../mall/goodscategory/findCategoryAllByParentId',
					data:{
						'id':$scope.selectedltype.id
					}
				}).success(function(data,status,headers,config){
					if(data.sucflag&&data.beanlist.length>0){
						$scope.errors={};
						$scope.stypeselect=[];
						$scope.stypeselect=data.beanlist;
						$scope.stypeidcategoryshow=true;
					}else{
						$scope.stypeidcategoryshow=false;
					}
				}).
				error(function(data,status,headers,config){
					$scope.errors={
						show:true,
						msg:'系统异常'
					}
				});

			};
		};


		/**
		 * 控制货物规格添加
		 */
		$scope.skuparams=[];
		$scope.index=1;
		$scope.addsku=function(){
			$scope.skuvals="";
			$scope.apsn="";//货号
			$scope.amemberprice="";//会员价
			$scope.asaleprice="";//销售价
			angular.forEach(angular.fromJson($scope.selectskus),function(d,i,a){
				$scope.skuvals+=d.skuval;
			});

			var param={id:++$scope.index,memberprice:$scope.amemberprice,saleprice:$scope.asaleprice,psn:$scope.apsn,skuvals:$scope.skuvals,selectskus:angular.fromJson($scope.selectskus),stock:1};
			$scope.skuparams.push(param);
			$scope.selectskus=[];
			$scope.selectskusids=[];
		};
		$scope.remove=function(index){
			$scope.skuparams.splice(index,1);
		}

		$scope.$watch('rmainfiles',function(){
			$scope.uploadrmainfiles($scope.rmainfiles);
		});
		$scope.allrmainfiles=[];
		$scope.uploadrmainfiles=function(files){
			if(files&&files.length){
				for(var i=0;i<files.length;i++){
					var file=files[i];
					var filename=files[i].name;
					if(!file.$error){
						Upload.upload({
							url:'../mall/files/ajaxFileUploadsImgs',
							data:{
								file:file,
								'filename':filename
							}
						}).progress(function (evt) {
							var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
							$scope.rmainprogress=progressPercentage;
						}).success(function (data, status, headers, config) {
							$timeout(function() {
								if(data.ifb.sucflag){
									$scope.allrmainfiles.push(data.ifb);
								}
							});
						});
					}
				}
			}
		}

		$scope.removeallrmainfiles=function(index){
			$scope.allrmainfiles.splice(index,1);
		}

		$scope.$watch('rfiles',function(){
			$scope.uploadrfiles($scope.rfiles);
		});
		$scope.allrfiles=[];
		$scope.uploadrfiles=function(files){
			if(files&&files.length){
				for(var i=0;i<files.length;i++){
					var file=files[i];
					var filename=files[i].name;
					if(!file.$error){
						Upload.upload({
							url:'../mall/files/ajaxFileUploadsImgs',
							data:{
								file:file,
								'filename':filename
							}
						}).progress(function (evt) {
							var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
							$scope.rprogress=progressPercentage;
						}).success(function (data, status, headers, config) {
							$timeout(function() {
								if(data.ifb.sucflag){
									$scope.allrfiles.push(data.ifb);
								}
							});
						});
					}
				}
			}
		};

		$scope.removeallrfiles=function(index){
			$scope.allrfiles.splice(index,1);
		};
		//选中sku值
		$scope.selectskusids=[];
		$scope.selectskus=[];
		var updateSelected=function(action,id,sku){
			if(action=='add'&& $scope.selectskusids.indexOf(id)==-1){
				$scope.selectskusids.push(id);
				$scope.selectskus.push(sku);
			}
			if(action=='remove'&& $scope.selectskusids.indexOf(id)!=-1){
				var idx= $scope.selectskusids.indexOf(id);
				$scope.selectskusids.splice(idx,1);
				$scope.selectskus.splice(idx,1);
			}
		}

		$scope.updateSelection=function($event,id,sku){
			var checkbox=$event.target;
			var action=(checkbox.checked?'add':'remove');
			updateSelected(action,id,sku);
		}

		$scope.isSelected=function(id){
			return $scope.selectskusids.indexOf(id)>=0;
		}

		//通过location中的operate参数区分操作行为
		var operate=$location.search().operate||$stateParams.operate;
		if(operate!=undefined&&operate=="save"){
			//查询所有货物规格
			$scope.findallproductsku=function(){
				var promise=productskuService.query();
				promise.then(function(data){
					$scope.skubeanlists=data.beanlist;
				},function(data){
					$scope.errors={
						show:true,
						msg:'系统异常'
					}
				});
			};

			//控制页面显示的信息
			$scope.tooglepdiv=false;

			//显示sku选择区域
			$scope.skushow=true;

			//控制保存按钮显示
			$scope.savebtn={show:true};
			$scope.title='添加货物';
			//隐藏上一个商品类型
			$scope.lastgoodstypenameshow=false;
			//隐藏修改商品类型按钮
			$scope.modifygoodstypeshow=false;
			//显示商品类型
			$scope.goodstypeshow=true;

			//隐藏上一个品牌
			$scope.lastbrandnameshow=false;
			$scope.modifybrandnameshow=false;
			$scope.brandnameshow=true;

			//隐藏上一个分类
			$scope.lastcategoryshow=false;
			$scope.modifycategoryshow=false;
			//显示一级分类选择
			$scope.navcategoryshow=true;
			//隐藏二级分类选择
			$scope.ltypecategoryshow=false;
			//隐藏三级分类选择
			$scope.stypeidcategoryshow=false;

			//预警库存默认10
			$scope.freezestock=10;
			//默认上架
			$scope.salestatus="1";
			//默认
			$scope.isdefault="1";
			//商品单位
			$scope.unit="1";
			//默认重量单位是公斤
			$scope.weightunit="1";
			//默认同步移动端
			$scope.msync="1";
			//设置启用 1表示启用
			$scope.status="1";
			//默认总点赞
			$scope.totalzan=100;
			//默认总评论
			$scope.totalcomment=100;
			//默认虚拟销量
			$scope.vsales=100;
			//默认真实销量
			$scope.sales=100;
			//默认排序
			$scope.sort=1;

			$scope.save=function(){

				if($scope.selectedgoodstype==undefined){
					$scope.errors={
						show:true,
						msg:'请选择商品类型'
					}
					return false;
				}
				if($scope.selectedbrands==undefined){
					$scope.errors={
						show:true,
						msg:'请选择品牌'
					}
					return false;
				}

				var psku="";//规格参数
				if($scope.skubeanlists!=undefined){
					if($scope.skuparams==undefined||$scope.skuparams.length==0){
						$scope.errors={
							show:true,
							msg:'请选择一个规格值'
						}
						return false;
					}
				}

				if($scope.selectednav==undefined){
					$scope.errors={
						show:true,
						msg:'请选择一级分类'
					}
					return false;
				}

				var ltypeid="";
				var lname="";
				if($scope.selectedltype!=undefined){
					ltypeid=$scope.selectedltype.id;
					lname=$scope.selectedltype.name;
				}

				var stypeid="";
				var sname="";
				if($scope.selectedstype!=undefined){
					stypeid=$scope.selectedltype.id;
					sname=$scope.selectedltype.name;
				}
				//if($scope.psn==undefined){
				//	$scope.errors={
				//		show:true,
				//		msg:'请输入货号'
				//	}
				//	return false;
				//}
                //
				if($scope.batchnumber==undefined){
					$scope.errors={
						show:true,
						msg:'请输入批次'
					}
					return false;
				}
				if($scope.productname==undefined){
					$scope.errors={
						show:true,
						msg:'请输入货物名称'
					}
					return false;
				}
				if($scope.weight==undefined){
					$scope.errors={
						show:true,
						msg:'请输入重量'
					}
					return false;
				}


				if($scope.points==undefined){
					$scope.errors={
						show:true,
						msg:'请输入可获积分'
					}
					return false;
				}

				if($scope.freezestock==undefined){
					$scope.errors={
						show:true,
						msg:'请输入预警库存'
					}
					return false;
				}
				if($scope.sort==undefined){
					$scope.errors={
						show:true,
						msg:'请输入排序'
					}
					return false;
				}
				if($scope.allrmainfiles.length==0){
					$scope.errors={
						show:true,
						msg:'请上传主图'
					}
					return false;
				}
				//if($scope.allrfiles.length==0){
				//	$scope.errors={
				//			show:true,
				//			msg:'请上传图集'
				//	}
				//	return false;
				//}
				$http({
					method:'POST',
					url:'../mall/products/save',
					data:{
						"goodstypeid":$scope.selectedgoodstype.goodstypeid,
						"goodstypename":$scope.selectedgoodstype.name,
						"brandid":$scope.selectedbrands.brandid,
						"brandname":$scope.selectedbrands.name,
						"pparams":angular.toJson($scope.gtparams),
						"pattrs":angular.toJson($scope.goodsattrs),
						"psku":angular.toJson($scope.skuparams),
						"navid":$scope.selectednav.id,
						"nname":$scope.selectednav.name,
						"ltypeid":ltypeid,
						"lname":lname,
						"stypeid":stypeid,
						"sname":sname,
						//"psn":$scope.psn,
						//"stock":$scope.stock,
						"batchnumber":$scope.batchnumber,
						"productname":$scope.productname,
						"weight":$scope.weight,
						"weightunit":$scope.weightunit,

						"memberprice":$scope.memberprice,
						"saleprice":$scope.saleprice,
						"points":$scope.points,
						"freezestock":$scope.freezestock,
						"totalzan":$scope.totalzan,
						"totalcomment":$scope.totalcomment,
						"unit":$scope.unit,
						"vsales":$scope.vsales,
						"sales":$scope.sales,
						"sort":$scope.sort,
						"msync":$scope.msync,
						"salestatus":$scope.salestatus,
						"status":$scope.status,
						"mainimg":angular.toJson($scope.allrmainfiles),
						"imgs":angular.toJson($scope.allrfiles)
					}
				})
					.success(function(data,status,headers,config){
						if(data.sucflag){
							$scope.errors={};
							$scope.info={
								show:true,
								msg:'货物添加成功'
							}
							$location.path('/productsment');
						}
					}).
				error(function(data,status,headers,config){
					$scope.errors={
						show:true,
						msg:'系统异常'
					}
				});

			}
		}



		if(operate=='edit'){
			var id=$location.search().id;
			if(id!=undefined&&id!=""){
				$http({
					method:'POST',
					url:'../mall/products/find',
					data:{
						'productid':id
					}
				}).success(function(data,status,headers,config){
					if(data.sucflag){
						//控制页面显示的信息
						$scope.tooglepdiv=true;

						$scope.title='正在对货物--'+data.bean.productname+'--进行编辑';
						//显示商品类型
						$scope.lastgoodstypenameshow=true;
						$scope.lastgoodstypename=data.bean.goodstypename;
						$scope.goodstypeid=data.bean.goodstypeid;
						//显示修改商品类型
						$scope.modifygoodstypeshow=true;
						//显示品牌
						$scope.lastbrandnameshow=true;
						$scope.lastbrandname=data.bean.brandname;
						$scope.brandid=data.bean.brandid;
						//重新查询可用的商品属性
						//$scope.findAllTypeGoodsAttrs(data.bean.goodstypeid);
						//显示修改品牌
						$scope.modifybrandnameshow=true;
						//货物参数
						$scope.gtparams=angular.fromJson(data.bean.pparams);
						//货物属性
						//$scope.goodsattrs=angular.fromJson(data.bean.pattrs);
						$scope.errors={};
						$scope.goodsattrs=[];
						$scope.attrslist=[];
						angular.forEach(angular.fromJson(data.bean.pattrs),function(d,i,a){
							$scope.attrslist.push(angular.fromJson(d.attributelist));
							$scope.goodsattrs.push(d);

						});


						//货物规格
						$scope.skubeanlists=angular.fromJson(data.bean.psku);
						angular.forEach($scope.skubeanlists,function(d,i,a){
							$scope.selectskusids.push(d.id);
						});
						$scope.selectskus=$scope.skubeanlists;
						//显示分类
						$scope.lastcategoryshow=true;
						$scope.lastcategory=data.bean.nname+"-"+data.bean.lname+"-"+data.bean.sname;
						$scope.navid=data.bean.navid;
						$scope.nname=data.bean.nname;
						$scope.ltypeid=data.bean.ltypeid;
						$scope.lname=data.bean.lname;
						$scope.stypeid=data.bean.stypeid;
						$scope.sname=data.bean.sname;
						//显示分类修改
						$scope.modifycategoryshow=true;
						$scope.psn=data.bean.psn;
						$scope.stock=data.bean.stock;
						//批次
						$scope.batchnumber=data.bean.batchnumber;
						$scope.productname=data.bean.productname;
						$scope.weight=data.bean.weight;
						$scope.weightunit=data.bean.weightunit;
						$scope.memberprice=data.bean.memberprice;
						$scope.saleprice=data.bean.saleprice;
						$scope.points=data.bean.points;
						$scope.freezestock=data.bean.freezestock;
						$scope.totalzan=data.bean.totalzan;
						$scope.totalcomment=data.bean.totalcomment;
						$scope.unit=data.bean.unit;
						$scope.vsales=data.bean.vsales;
						$scope.sales=data.bean.sales;
						$scope.sort=data.bean.sort;
						$scope.msync=data.bean.msync;
						$scope.salestatus=data.bean.salestatus;
						$scope.status=data.bean.status;
						$scope.allrmainfiles.push(data.ifb);
						$scope.allrfiles=data.ifbs;
						$scope.updatebtn={show:true};
					}
				}).error(function(data,status,headers,config){
					$scope.errors={
						show:true,
						msg:'系统异常'
					}
				});
			}

			//点击修改商品类型按钮
			$scope.clickmodifygoodstype=function(){
				$scope.goodstypeshow=true;
			}
			//点击修改品牌
			$scope.clickmodifybrandname=function(){
				$scope.brandnameshow=true;
			}
			//点击修改分类
			$scope.clickmodifycategory=function(){
				$scope.navcategoryshow=true;
			}
			//更新货物
			$scope.update=function(){
				var goodstypeid="";
				var goodstypename="";
				if($scope.goodstypeshow){
					if($scope.selectedgoodstype==undefined){
						$scope.errors={
							show:true,
							msg:'请选择商品类型'
						}
						return false;
					}else{
						goodstypeid=$scope.selectedgoodstype.goodstypeid;
						goodstypename=$scope.selectedgoodstype.name;
					}
				}else{
					goodstypeid=$scope.goodstypeid;
					goodstypename=$scope.lastgoodstypename;
				}
				var brandid="";
				var brandname="";
				if($scope.brandnameshow){
					if($scope.selectedbrands==undefined){
						$scope.errors={
							show:true,
							msg:'请选择品牌'
						}
						return false;
					}else{
						brandid=$scope.selectedbrands.brandid;
						brandname=$scope.selectedbrands.name;
					}
				}else{
					brandid=$scope.brandid;
					brandname=$scope.lastbrandname;
				}

				var psku="";//规格参数
				if($scope.skubeanlists!=undefined){
					if($scope.selectskus==undefined||$scope.selectskus.length==0){
						$scope.errors={
							show:true,
							msg:'请选择一个规格值'
						}
						return false;
					}
				}
				if($scope.navcategoryshow){
					var navid="";
					var nname="";
					if($scope.selectednav==undefined){
						$scope.errors={
							show:true,
							msg:'请选择一级分类'
						}
						return false;
					}else{
						navid=$scope.navid;
						nname=$scope.nname;
					}
					var ltypeid="";
					var lname="";
					if($scope.selectedltype!=undefined){
						ltypeid=$scope.selectedltype.id;
						lname=$scope.selectedltype.name;
					}else{
						ltypeid=$scope.ltypeid;
						lname=$scope.lname;
					}

					var stypeid="";
					var sname="";
					if($scope.selectedstype!=undefined){
						stypeid=$scope.selectedltype.id;
						sname=$scope.selectedltype.name;
					}else{
						stypeid=$scope.stypeid;
						sname=$scope.sname;
					}
				}else{
					var navid=$scope.navid;
					var nname=$scope.nname;
					var ltypeid=$scope.ltypeid;
					var lname=$scope.lname;
					var stypeid=$scope.stypeid;
					var sname=$scope.sname;
				}

				if($scope.batchnumber==undefined){
					$scope.errors={
						show:true,
						msg:'请输入批次'
					}
					return false;
				}

				if($scope.psn==undefined){
					$scope.errors={
						show:true,
						msg:'请输入货号'
					}
					return false;
				}

				if($scope.stock==undefined){
					$scope.errors={
						show:true,
						msg:'请输入库存'
					}
					return false;
				}
				if($scope.productname==undefined){
					$scope.errors={
						show:true,
						msg:'请输入货物名称'
					}
					return false;
				}
				if($scope.weight==undefined){
					$scope.errors={
						show:true,
						msg:'请输入重量'
					}
					return false;
				}

				if($scope.memberprice==undefined){
					$scope.errors={
						show:true,
						msg:'请输入会员价格'
					}
					return false;
				}

				if($scope.saleprice==undefined){
					$scope.errors={
						show:true,
						msg:'请输入销售价格'
					}
					return false;
				}
				if($scope.points==undefined){
					$scope.errors={
						show:true,
						msg:'请输入可获积分'
					}
					return false;
				}

				if($scope.freezestock==undefined){
					$scope.errors={
						show:true,
						msg:'请输入预警库存'
					}
					return false;
				}
				if($scope.sort==undefined){
					$scope.errors={
						show:true,
						msg:'请输入排序'
					}
					return false;
				}
				if($scope.allrmainfiles.length==0){
					$scope.errors={
						show:true,
						msg:'请上传主图'
					}
					return false;
				}
				//if($scope.allrfiles.length==0){
				//	$scope.errors={
				//			show:true,
				//			msg:'请上传图集'
				//	}
				//	return false;
				//}


				$http({
					method:'POST',
					url:'../mall/products/update',
					data:{
						"goodstypeid":goodstypeid,
						"goodstypename":goodstypename,
						"brandid":brandid,
						"brandname":brandname,
						"pparams":angular.toJson($scope.gtparams),
						"pattrs":angular.toJson($scope.goodsattrs),
						"psku":angular.toJson($scope.selectskus),
						"navid":navid,
						"nname":nname,
						"ltypeid":ltypeid,
						"lname":lname,
						"stypeid":stypeid,
						"sname":sname,
						"psn":$scope.psn,
						"stock":$scope.stock,
						"batchnumber":$scope.batchnumber,
						"productname":$scope.productname,
						"weight":$scope.weight,
						"weightunit":$scope.weightunit,
						"memberprice":$scope.memberprice,
						"saleprice":$scope.saleprice,
						"points":$scope.points,
						"freezestock":$scope.freezestock,
						"totalzan":$scope.totalzan,
						"totalcomment":$scope.totalcomment,
						"unit":$scope.unit,
						"vsales":$scope.vsales,
						"sales":$scope.sales,
						"sort":$scope.sort,
						"msync":$scope.msync,
						"salestatus":$scope.salestatus,
						"status":$scope.status,
						"mainimg":angular.toJson($scope.allrmainfiles),
						"imgs":angular.toJson($scope.allrfiles),
						"productid":id
					}
				})
					.success(function(data,status,headers,config){
						if(data.sucflag){
							$scope.errors={};
							$scope.info={
								show:true,
								msg:'货物更新成功'
							}
							$location.path('/productsment');
						}else {
							$scope.errors={
								show:true,
								msg:data.msg
							}
						}
					}).
				error(function(data,status,headers,config){
					$scope.errors={
						show:true,
						msg:'系统异常'
					}
				});

			}

		}
	}



	productsmodule.controller('productsment',ServerProductsmentCtrl);
	//查询列表数据
	function ServerProductsmentCtrl($http,$location,$compile,$scope,$rootScope,$resource,DTOptionsBuilder,DTColumnBuilder,DTAjaxRenderer){
		$scope.title="货物列表";
		var vm=this;
		vm.message='';
		vm.someClickHandler = someClickHandler;
		vm.selected={};
		vm.selectAll=false;
		vm.toggleAll=toggleAll;
		vm.toggleOne=toggleOne;
		vm.dtInstance={};
		var titleHtml='<input type="checkbox" ng-model="showCase.selectAll" ng-click="showCase.toggleAll(showCase.selectAll,showCase.selected)">';
		vm.dtOptions=DTOptionsBuilder.newOptions().withOption('ajax',{
			type:'POST',
			url:'../mall/products/findByPage',
			dataSrc:'data'
		})
		.withOption('processing',true)
		.withOption('paging',true)
		.withOption('serverSide',true)
		.withOption('createdRow',function(row,data,dataIndex){
			$compile(angular.element(row).contents())($scope);
		})
		.withOption('headerCallback', function(header) {
            if (!$scope.headerCompiled) {
                $scope.headerCompiled = true;
                $compile(angular.element(header).contents())($scope);
            }
        })
        .withOption('stateSave', true)
        .withOption('rowCallback',rowCallback)
		.withPaginationType('full_numbers')
		.withLanguageSource('./app/language/chinese.json')

		$scope.$on('handleRequest',function(){

		});
		vm.dtColumns=[
		              DTColumnBuilder.newColumn(null).withTitle(titleHtml).notSortable().renderWith(function(data,type,full,meta){
		            	  vm.selected[full.id]=false;
		            	  return "<input type='checkbox' ng-model=\"showCase.selected['"+data.id+"']\" ng-click=\"showCase.toggleOne(showCase.selected)\">";
		              }),
		              DTColumnBuilder.newColumn('id').withTitle('ID').notVisible(),
		              DTColumnBuilder.newColumn(null).withTitle('图片').notSortable().renderWith(imgHtml),
			          DTColumnBuilder.newColumn('psn').withTitle('货号').notSortable(),
			          DTColumnBuilder.newColumn('productname').withTitle('货物名称').notSortable(),
			          DTColumnBuilder.newColumn('nname').withTitle('分类').notSortable(),
			          DTColumnBuilder.newColumn('goodstypename').withTitle('商品类型').notSortable(),
			          DTColumnBuilder.newColumn('points').withTitle('积分').notSortable(),
			          DTColumnBuilder.newColumn('saleprice').withTitle('销售价').notSortable(),
			          DTColumnBuilder.newColumn('memberprice').withTitle('会员价').notSortable(),
			          DTColumnBuilder.newColumn('weight').withTitle('重量').notSortable(),
			          DTColumnBuilder.newColumn('salestatus').withTitle('销售状态').notSortable(),
			          DTColumnBuilder.newColumn('isdefault').withTitle('首选状态').notSortable(),
			          DTColumnBuilder.newColumn('msync').withTitle('移动端状态').notSortable(),
			          DTColumnBuilder.newColumn('goodsid').withTitle('主体状态').notSortable(),
								DTColumnBuilder.newColumn('sort').withTitle('排序').notSortable(),
			          DTColumnBuilder.newColumn('status').withTitle('状态').notSortable(),
			          DTColumnBuilder.newColumn('updatetime').withTitle('更新时间').notSortable(),
			          DTColumnBuilder.newColumn('version').withTitle('版本号').notSortable(),
			          DTColumnBuilder.newColumn(null).withTitle('操作').notSortable().renderWith(actionHtml)];
		function imgHtml(data,type,full,meta){
			return '<img style="width:50px;height:50px;" ng-src="'+data.smainimg+'" alt="Photo" />';
		}
		
		function actionHtml(data,type,full,meta){
			return "<button  ng-click=\"edit('"+data.id+"')\" class='btn btn-warning'><i class='fa fa-edit'></i></button>";
		}

		function reloadData(){
			var resetPaging = false;
	        vm.dtInstance.reloadData(callback, resetPaging);
		}
		function callback(data){
		}


		//表格中编辑按钮
		$scope.edit=function(id,name){
			$location.path('/products').search({'operate':'edit','id':id});
		}
		/**
		 * 复杂搜索刷新数据
		 */
		function searchReloadData(){
			var resetPaging=false;
			var searchParams={
				isCustomSearch:1,
				name:''
			};
			if($scope.productname!=undefined){
				searchParams.name=$scope.productname;
				vm.dtOptions['ajax'].data=searchParams;
				vm.dtInstance.reloadData(callback, resetPaging);
			}
		}
		/**
		 * 表格
		 */
		$scope.searchdataTableCustom=function(){
			searchReloadData();
		}

		//表格中编辑按钮
		//$scope.edit=function(id,name){
		//	$location.path('/products').search({'operate':'edit','id':id});
		//}

		/**
		 * 跳转到添加商品类型和参数页面
		 */
		$scope.save=function(){
			$location.path('/products').search({'operate':'save'});
		}
		$scope.del=function(){
			var i=0;
			var ids=[];
			angular.forEach(vm.selected, function(data,index,array){
				if(data){
					i++;
					ids.push(index);
				}
			});
			if(i==0){
				$scope.errors={
						show:true,
						msg:'请选择一条记录'
				}
			}else{
				$scope.errors={
						show:false
				}
				//批量删除数据
				var idstrs=ids.join(",");
				$http({
					method:'POST',
					url:'../mall/products/del',
					data:{
						'productid':idstrs
					}
				}).
				success(function(data,status,headers,config){
					if(data.sucflag){
						$scope.errors={};
						$scope.info={
								show:true,
								msg:'删除成功'
						}
						reloadData();
					}

				}).
				error(function(data,status,headers,config){
					$scope.errors={
							show:true,
							msg:'系统异常'
					}
				});
			}
		}

		/**
		 * 列表全选
		 */
		function toggleAll(selectAll,selectedItems){
			for(var id in selectedItems){
				if(selectedItems.hasOwnProperty(id)){
					selectedItems[id]=selectAll;
				}
			}
		}
		/**
		 * 列表单选
		 */
		function toggleOne(selectedItems){
			var me=this;
			for(var id in selectedItems){
				if(selectedItems.hasOwnProperty(id)){
					if(!selectedItems[id]){
						me.selectAll=false;
					}
				}
			}
			me.selectAll=true;
		}

		function someClickHandler(info) {
	        vm.message = info.id + ' - ' + info.name;
	    }
		/**
		 * 单击列表某行回调
		 */
		function rowCallback(nRow,aData,iDisplayIndex,iDisplayIndexFull){
			$('td', nRow).unbind('click');
	        $('td', nRow).bind('click', function() {
	            $scope.$apply(function() {
	                vm.someClickHandler(aData);
	            });
	        });
	        return nRow;
		}

	}
});
